package com.zhn.evsi.interceptor;

import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dtflys.forest.annotation.URLEncode;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.interceptor.Interceptor;
import com.zhn.evsi.client.CharacterClient;
import com.zhn.evsi.client.CharacterPublicClient;
import com.zhn.evsi.common.RedisCache;
import com.zhn.evsi.domain.dto.System.ErrorDTO;
import com.zhn.evsi.domain.dto.evsi.character.EvsiCharacterPublicInfoDTO;
import com.zhn.evsi.domain.entity.System.SysUser;
import com.zhn.evsi.service.IEvsiAuthTokenService;
import com.zhn.evsi.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;

/**
 * token拦截器
 * */
@Component
public class TokenInterceptor implements Interceptor<ErrorDTO> {

    @Autowired
    private IEvsiAuthTokenService IEvsiAuthTokenService;

    @Autowired
    private CharacterInterceptor characterInterceptor;


    /**
     * token获取
     * */
    @Override
    public boolean beforeExecute(ForestRequest request) {

        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);

        String token = IEvsiAuthTokenService.oauthTokenV2(characters.getCharacters());
//        String encode = URLUtil.encode(token);
        try {
            URLEncoder.encode(token, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        request.replaceOrAddQuery("token", token);
        return true;
    }
}
